package com.jr.dao.impl;

import com.jr.dao.OrderDao;
import com.jr.entity.Order;
import com.jr.util.DAO;

import java.util.List;

public class OrderDaoImpl extends DAO<Order> implements OrderDao {
    @Override
    public List<Order> listAllOrder(Integer enterpriseId) throws Exception {
        String sql = "SELECT no ,total_amount,create_time FROM `order` WHERE exist = 1 AND enterprise_id = ?";
        return getForList(sql, enterpriseId);
    }

    @Override
    public long getTotal(Integer enterpriseId) throws Exception {
        String sql = "select count(*) from `order` where exist = 1 AND enterprise_id = ?";
        return getForValue(sql, enterpriseId);
    }

    @Override
    public List<Order> findOrder(Integer enterpriseId, int start, int pageSize) throws Exception {
        String sql = "SELECT no ,total_amount,create_time FROM `order` WHERE exist = 1 AND enterprise_id = ? LIMIT ?,?";
        return getForList(sql, enterpriseId, start, pageSize);
    }

    @Override
    public int deleteOrder(String no) throws Exception {
        String sql = "UPDATE `order` SET exist = 0 WHERE `no`= ? ";
        return update(sql, no);
    }

    /**
     * 根据发票id查找所有订单
     *
     * @param invoiceId
     * @return
     * @throws Exception
     */
    @Override
    public List<Order> invoiceOrder(Integer invoiceId) throws Exception {
        String sql = "SELECT no,enterprise_name,total_amount,create_time FROM `order` WHERE invoicing_record_id=?";
        return getForList(sql,invoiceId);
    }

    /**
     * 根据发票id计算开票总金额
     * @param invoiceId
     * @return
     * @throws Exception
     */
    @Override
    public Double TotalAmount(Integer invoiceId) throws Exception {
        String sql = "SELECT SUM(total_amount) FROM `order` WHERE invoicing_record_id=?";
        return getForValue(sql,invoiceId);
    }

    /**
     * 订单总金额
     * @return
     * @throws Exception
     */
    @Override
    public Double sumAmount() throws Exception {
        String sql = "SELECT SUM(total_amount) FROM `order`";
        return getForValue(sql);
    }

    /**
     * 根据开票状态计算已开票的订单总金额
     * @param invoiceStatus
     * @return
     * @throws Exception
     */
    @Override
    public Double finishSumAmount() throws Exception {
        String sql = "SELECT SUM(total_amount) FROM `order` WHERE invoicing_status=1";
        return getForValue(sql);
    }

    @Override
    public int updateOrder(String no) throws Exception {
        String sql ="UPDATE `order` SET invoicing_record_id = (SELECT id FROM invoicing_record WHERE exist = 1) WHERE `no` = ?";
        return update(sql,no);
    }

    @Override
    public int updateStatus(String no) throws Exception {
        String sql="UPDATE `order` SET invoicing_status = 1 WHERE `no` = ?";
        return update(sql,no);
    }

    /**
     * 根据id进行退票
     * @param invoiceId
     * @return
     * @throws Exception
     */
    @Override
    public int refundTicket(Integer invoiceId) throws Exception {
        String sql="UPDATE `order` SET invoicing_status=0, invoicing_record_id=0 , exist=1 WHERE invoicing_record_id=?";
        return update(sql,invoiceId);
    }


}
